﻿
@{

}

<div class="mini-toolbar" style="padding: 2px; border: 0;">
    <table style="width:100%;">
        <tr>
            <td style="width:100%;">
                <a id="a_refresh" plain="true" class="mini-button" iconCls="icon-refresh" title="刷新" onClick="refreshClick()">刷新</a>
                <span class="separator"></span>
                @* 列表按钮  *@
                @Html.ToolbarButton("Sys.Job.Index")
            </td>
            <td style="white-space:nowrap;">
                <label style="font-family:Verdana;">任务名称： </label>
                <input class="mini-textbox" id="txtKeyword" name="txtKeyword" /> 
                <a class="mini-button" iconCls="icon-search" plain="true" onclick="onSearch()">查询</a>
            </td>
        </tr>
    </table> 
</div>
<div id="grid" class="mini-datagrid" style="width: 100%; height: 94%;" url="/sys/job/IndexAjax" allowResize="true" idField="name" onrowdblclick="rowdblclick" 
     ondrawcell="drawcell">
    <div property="columns">
        <div type="indexcolumn"></div>
        <div type="checkcolumn"></div>
        <div field="Name" align="left" width="120" headeralign="center">名称</div>
        <div field="JobType" align="left" width="120" headeralign="center">工作类</div>
        <div field="Parameters" align="center" width="120" headeralign="center">参数</div>
        <div field="Description" align="left" width="120" headeralign="center">描述</div>
        <div field="Status" align="center" width="120" headeralign="center">状态</div>
        <div field="NextFireTimeUtc" align="center" width="120" headeralign="center" dateFormat="yyyy-MM-dd HH:mm:ss">下次执行时间</div>
        <div field="FireTimeUtc" align="center" width="120" headeralign="center" dateFormat="yyyy-MM-dd HH:mm:ss">执行时间</div>
        <div field="PreviousFireTimeUtc" align="center" width="120" headeralign="center" dateFormat="yyyy-MM-dd HH:mm:ss">上次执行时间</div>
        <div field="JobRunTime" align="center" width="120" headeralign="center">执行用时(毫秒)</div>
        <div field="CreateTime" align="center" width="120" headeralign="center" dateFormat="yyyy-MM-dd HH:mm:ss">创建时间</div>
    </div>
</div>

<div id="winAdd" class="mini-window" title="新增" style="width: 900px;" showmodal="true" allowresize="true" showCloseButton="false">
    <form id="winAddForm">
        <table style="width: 100%;">
            <tr>
                <td style="display:none"><input id="Id" name="id" class="mini-textbox" /></td>
                <td>名称:</td>
                <td>
                    <input id="Name" name="name" textname="Name" class="mini-textbox" style="width: 160px;" required="true" />
                </td>
                <td>描述:</td>
                <td>
                    <input class="mini-textbox" id="Description" name="description" style="width: 160px;" required="true" />
                </td>
                <td>工作类:</td>
                <td>
                    <input class="mini-textbox" id="JobType" name="jobType" style="width: 380px;" required="true" />
                </td>

            </tr>
            <tr>
                <td>状态:</td>
                <td>
                    <input id="Status" name="status" class="mini-combobox" textfield="name" style="width: 160px;" valuefield="id"
                           data="[{id:0,name:'不可用'}, {id:1,name:'可用'}, { id: 4, name: '暂停中' }, { id: 5, name: '暂停' }]" value="0" allowinput="false" />
                </td>
                <td>参数:</td>
                <td>
                    <input class="mini-textbox" id="Parameters" name="parameters" style="width: 160px;" />
                </td>
                <td></td>
                <td>
                    <input class="mini-button" text="新增触发器" style="width: 120px;" onclick="doAdd()" />
                    <input class="mini-button" text="删除触发器" style="width: 120px;" onclick="doDelete()" />
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <div id="win_Grid" class="mini-datagrid" style="width: 100%; height: 450px;" allowResize="true" idField="id" showpager="false" allowCellEdit="true" allowCellSelect="true" ondrawcell="drawcell" oncellendedit="cellendedit">
                        <div property="columns">
                            <div type="indexcolumn"></div>
                            <div type="checkcolumn"></div>
                            <div field="name" align="center" width="120" headeralign="center">
                                触发器名
                                <input property="editor" class="mini-textbox" style="width:100%;" />
                            </div>
                            <div field="status" align="center" width="120" headeralign="center">
                                状态
                                <input property="editor" class="mini-combobox" style="width:100%;" data="Status" />
                            </div>
                            <div field="cronExpression" align="center" width="120" headeralign="center">
                                Cron表达式
                                <input property="editor" class="mini-textbox" style="width:100%;" />
                            </div>
                            <div field="description" align="center" width="120" headeralign="center" allowsort="true">Cron描述</div>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: right; padding-top: 5px; padding-right: 20px;" colspan="6">
                    <input class="mini-button" id="btnSave" text="保存" onclick="doSave" />
                    <input class="mini-button" text="关闭" onclick="win_Cancel" />
                </td>
            </tr>
        </table>
    </form>
</div>

<div id="winLog" class="mini-window" title="作业日志" style="width: 1000px;" showmodal="true" allowresize="true" showCloseButton="false">
    <form id="winAddFormLog">
        <table style="width: 100%;">
            <tr>
                <td colspan="6">
                    <input class="mini-hidden" id="LogId" name="LogId" type="hidden" />
                    <input class="mini-button" text="刷新" style="width: 120px;" onclick="doRefresh()" />
                    <input class="mini-button" text="删除" style="width: 120px;" onclick="doDeleteLog()" />
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <div id="LogGrid" class="mini-datagrid" style="width: 100%; height: 500px;" allowResize="true" idField="id" url="/sys/job/logajax"  multiSelect="true" >
                        <div property="columns">
                            <div type="indexcolumn"></div>
                            <div type="checkcolumn"></div>
                            <div field="JobName" align="center" width="100" headeralign="center" allowsort="true">作业名称</div>
                            <div field="Content" align="center" width="100" headeralign="center" allowsort="true"> 日志内容</div>
                            <div field="ExceptionMessage" align="center" width="100" headeralign="center" allowsort="true">异常信息</div>
                            <div field="NextFireTimeUtc" align="center" width="100" headeralign="center" allowsort="true" dateFormat="yyyy-MM-dd HH:mm:ss">下次执行时间</div>
                            <div field="FireTimeUtc" align="center" width="100" headeralign="center" allowsort="true" dateFormat="yyyy-MM-dd HH:mm:ss">执行时间</div>
                            <div field="PreviousFireTimeUtc" align="center" width="100" headeralign="center" allowsort="true" dateFormat="yyyy-mm-dd HH:MM:ss">上次执行时间</div>
                            <div field="JobRunTime" align="center" width="100" headeralign="center" allowsort="true">执行用时(毫秒)</div>
                            <div field="CreateTime" align="center" width="100" headeralign="center" allowsort="true" dateFormat="yyyy-MM-dd HH:mm:ss">创建时间</div>
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: right; padding-top: 5px; padding-right: 20px;" colspan="6">
                    <input class="mini-button" text="关闭" onclick="winLogCancel" />
                </td>
            </tr>
        </table>
    </form>
</div>

<script>
    mini.parse();
    var Status = [{ id: 0, text: '不可用' }, { id: 1, text: '可用' }];

    $(function () {
        doLoad();
    });

    function doLoad() {
        var grid = mini.get("grid");
        var keyword = mini.get("txtKeyword").getValue();
        grid.load({ Name:keyword});
    }

    function refreshClick() {
        doLoad();
    }

    function addClick() {
        $("#Id").val("");
        var win = mini.get("winAdd");
        win.show();
    }

    function win_Cancel() {
        var win = mini.get("winAdd");
        var form = new mini.Form("#winAddForm");
        var win_Grid = mini.get("win_Grid");
        win_Grid.clearRows();
        form.clear();
        win.hide();
    }

    //编辑
    function rowdblclick(e) {
        addClick();
        var record = e.record;
        $("#Id").val(record.Id);
        $.ajax({
            type: "post",
            url: "/sys/job/getjob",
            data: { id: record.Id },
            dataType: "json",
            success: function (d) { 
                var form = new mini.Form("#winAddForm"); 
                form.setData(d.data);
                var win_Grid = mini.get("win_Grid");
                win_Grid.addRows(d.data.trigger, 0);
            }
        });
    }

    function deleteClick() {
        var selected = mini.get("grid").getSelected();
        if (selected) {
            $.ajax({
                type: "post",
                url: "/sys/job/delete",
                data: { id: selected.Id },
                dataType: "json",
                success: function (d) {
                    if (d.info == "操作成功！") {
                        doLoad();
                    }
                    else {
                        mini.alert("操作失败!");
                    }
                }
            });
        }
        else {
            mini.alert("请选择一条数据！");
        }
    }

    function drawcell(e) {
        var record = e.record;
        var field = e.field;
        if (field == "Status") {
            if (record.Status == 0) {
                e.cellHtml = "不可用";
            }
            else if (record.Status == 1) {
                e.cellHtml = "可用";
            }
            else if (record.Status == 5) {
                e.cellHtml = "暂停";
            }
        }
    }

    function doAdd() {
        var grid = mini.get("win_Grid")
        var index = grid.getData().length;
        var data = { Id: "", JobId: $("#Id").val(), Name: "", Status: 0, Description: "", CronExpression: "" }
        grid.addRow(data, index + 1);
    }

    //删除触发器
    function doDelete() {
        var grid = mini.get("win_Grid");
        var row = grid.getSelected();
        if (row == undefined) {
            mini.alert("请先选择一行数据!");
            return;
        }
        grid.removeRow(row);
    }

    function cellendedit(e) {
        var record = e.record;
        var field = e.field;
        if (field == "Name") {
            record.Name = e.value;
        }
        else if (field == "CronExpression") {
            record.CronExpression = e.value;
        }
        else if (field == "Status") {
            record.Status = e.value;
        }
        var grid = mini.get("win_Grid");
        grid.updateRow(grid.getSelected());
    }

    function doSave() {
        var form = new mini.Form("#winAddForm");
        var data = form.getData();
        if (data.Name == "" || data.JobType == "" || data.Description == "") {
            mini.alert("名称、工作类、描述必填!");
            return;
        }
        data.trigger = mini.get("win_Grid").getData();
        var isSave = true;
        if (data.trigger.length > 0) {
            $.each(data.trigger, function (i, n) {
                if (n.name == "" || n.cronExpression == "") {
                    mini.alert("触发器名、Cron表达式必填！");
                    isSave = false;
                    return;
                }
            });
        }
        else {
            mini.alert("作业必须有一个触发器！");
            isSave = false;
        }
        if (!isSave) {
            return;
        }
        var json = JSON.stringify(data);
        var messageid = mini.loading("保存中,请稍候 ...", "提示");
        $.ajax({
            url: "/sys/job/editajax",
            data: {
                json: json,
            },
            dataType: "json",
            type: "POST",
            error: function (r) {
                mini.hideMessageBox(messageid);
                console.log(r, JSON.parse(r));
            },
            success: function (d) {
                mini.hideMessageBox(messageid);
                mini.alert(d.info);
                if (d.info == "操作成功！") {
                    win_Cancel();
                    doLoad();
                }
            }
        });
    }

    function runClick() {
        var selected = mini.get("grid").getSelected();
        if (selected) {
            if (selected.Status == 0) {
                mini.alert("作业当前不可用！");
                return;
            }
            var messageid = mini.loading("运行中,请稍候 ...", "提示");
            $.ajax({
                type: "post",
                url: "/sys/job/runone",
                data: { id: selected.Id },
                dataType: "json",
                error: function (r) {
                    mini.hideMessageBox(messageid);
                },
                success: function (d) {
                    mini.hideMessageBox(messageid);
                    if (d.info == "操作成功！") {
                        mini.alert("操作成功！");
                        doLoad();
                    }
                    else {
                        mini.alert("运行失败！");
                    }
                }
            });
        }
        else {
            mini.alert("请选择一条数据！");
        }
    }

    function readLogClick() {
        var selected = mini.get("grid").getSelected();
        if (selected) {
            mini.get("LogId").setValue(selected.Id);
            mini.get("LogGrid").load({ id: selected.Id })
            var win = mini.get("winLog");
            win.show();
        }
        else {
            mini.alert("请选择一条数据！");
        }
    }

    function winLogCancel() {
        var win = mini.get("winLog");
        var form = new mini.Form("#winAddFormLog");
        var win_Grid = mini.get("LogGrid");
        win_Grid.clearRows();
        form.clear();
        win.hide();
    }

    function doRefresh() {
        mini.get("LogGrid").load({ id: mini.get("LogId").getValue() });
    }

    function doDeleteLog() {
        var rows = mini.get("LogGrid").getSelecteds();
        if (rows && rows.length) {

            var ids = "";
            for (var i = 0, l = rows.length; i < l; i++) {
                var row = rows[i];
                ids += row.Id;
                if (i != l - 1) ids += ",";
            } 
            $.ajax({
                type: "post",
                url: "/sys/job/LogDelete",
                data: { ids: ids },
                dataType: "json",
                success: function (d) {
                    if (d.info == "操作成功！") {
                        doRefresh();
                    }
                    else {
                        mini.alert("删除失败！");
                    }
                }
            });
        }
        else {
            mini.alert("请选择一条数据！");
        }
    }

    function doDelete() {
        var win_Grid = mini.get("win_Grid");
        var selected = win_Grid.getSelected();
        win_Grid.removeRow(selected);
    }
</script>
